import { reqAddOrUpdateShopCart, reqGoodsInfo } from "@/api"
import { getUUID } from "@/utils/uuid_token"

const actions = {
    async getGoodsInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code == 200) {
            commit('GetGoodsInfo', result.data)
        }
    },
    // eslint-disable-next-line no-unused-vars
    async addOrUpdate({ commit }, { skuId, skuNum }) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum)
        // 请求API只是为了让服务器保存起来，下次用，因此返回的data不需要存下来，因为也是null
        // console.log(result);
        if(result.code==200){
            return 'OK'
        }else{
            return Promise.reject(new Error('Failed!'))
        }
    }
}

const mutations = {
    GetGoodsInfo(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    }
}

const getters = {
    // 导航目录
    categoryView() {
        return state.goodsInfo.categoryView || {}
    },
    // 产品详细信息
    skuInfo() {
        return state.goodsInfo.skuInfo || {}
    },
    // 产品属性
    spuSaleAttrList() {
        return state.goodsInfo.spuSaleAttrList || {}
    }
}

const state = {
    goodsInfo: {},
    uuid_token: getUUID()
}

export default {
    actions,
    mutations,
    getters,
    state
}